import ala from 'ala'
import api from 'api'
import Vue from 'vue'

// 删除
export async function del (array, row) {
  if (row.parentId > 0) {
    ala.error('存在子标签不能删除')
  }
  Vue.prototype.$confirm(`是否删除标签:${row.name}?`, {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    var response = await ala.httpDelete('api/Relation/Delete?id=' + row.id)
    if (response) {
      array = deleteArray(array, row)
      ala.toast('标签' + row.name + '删除成功')
    }
  })
  return array
}

function deleteArray (array, row) {
  array.forEach((one, index) => {
    if (row.fatherId === 0) {
      if (one.id === row.id) {
        array.splice(index, 1)
      }
    } else {
      if (one.id === row.fatherId && one.children) {
        one.children.forEach((two, twoIndex) => {
          if (two.id === row.id) {
            one.children.splice(twoIndex, 1)
          }
        })
      }
    }
  })
}